home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c.moderated,comp.std.c
- Subject: Re: 'h' modifier in printf
- Date: 14 Mar 1996 21:14:01 -0600
- Organization: CERN European Lab for Particle Physics
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4ian9p$h5t@solutions.solon.com>
- References: <4i801c$455@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4i801c$455@solutions.solon.com> tada@athena.mit.edu (Michael J Zehr) writes:
-
- >The "h" modifier says the corresponding argument will be printed as a
- >short or unsigned short.
- >
- >So, given:
- >
- >short s;
- >printf("%d", s);
- >printf("%hd", s);
- >
- >(Assuming of course that s has been initialized at some point.)
- >
- >Can these two ever be different?
-
- No.
-
- >This is the main question I'm interested, but as a followup, if these
- >always result in the same output, why is the 'h' modifier defined in the
- >first place?
-
- Because there are other examples where the output is actually changed by
- the 'h' modifier:
-
- int i = SHRT_MAX + 1; /* assuming that INT_MAX > SHRT_MAX */
- short s = -1;
-
- printf("%d", i);
- printf("%hd", i);
-
- printf("%x", (unsigned)s);
- printf("%hx", (unsigned)s);
-
- >Some speculations:
- >
- >int i;
- >printf("%hd", i);
- >printf("%d", (short)i);
- >
- >This question is stretching a bit to try to find what if anything the
- >'h' modifier is ever used for. Certainly the first line would have a
- >different result without the 'h', but casting seems like it ought to
- >have the same result.
-
- You have just discovered that there's more than one way to achieve the
- same result in C :-) The second method involves two conversions, while
- the first one needs only one (inside printf).
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-